const fs = require('fs');
const service = require('../services/users.service');
const FileService = require("../services/file.service");
const {AVATAR_PATH} = require('../constant/path');

class UsersController {
  async create(ctx, next) {
    //获取参数
    //查询数据库放到service中
    //插入数据库
    //返回结果
    //将avatar保存到user表中
    ctx.body = await service.create(ctx.request.body);
  }

  async avatarInfo(ctx, next) {
    const {userId} = ctx.params

    //找到用户头像文件
    const avatar = await service.getAvatarById(userId)
    // //将avatar保存到user表中
    ctx.response.set('content-type', avatar.mimetype);
    ctx.body = fs.createReadStream(`${AVATAR_PATH}/${avatar.filename}`)
  }
}

module.exports = new UsersController();